package com.xzq.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzq.erp.domain.dto.OrderDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.po.Customer;
import com.xzq.erp.domain.po.Order;
import com.xzq.erp.domain.po.Product;
import com.xzq.erp.domain.query.OrderPageQuery;
import com.xzq.erp.domain.vo.OrderVO;
import com.xzq.erp.domain.vo.SalesOpportunityVO;
import com.xzq.erp.mapper.OrderMapper;
import com.xzq.erp.service.ICustomerService;
import com.xzq.erp.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzq.erp.service.IProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    private final ICustomerService customerService;

    private final IProductService productService;

    @Override
    public PageDTO<OrderVO> getOrderList(OrderPageQuery query) {
        PageHelper.startPage(query.getCurrent(), query.getSize());
        // 注意Page继承了ArrayList，所以它是个集合对象
        Page<OrderVO> page = this.baseMapper.getOrderList(query);// 查询分页查询列表
        List<OrderVO> result = page.getResult();
        if(CollectionUtils.isEmpty(result)){
            return PageDTO.empty(page.getTotal(), (long)page.getPages());
        }
        log.info("查询结果:{},{}", page.getPages(), page.getTotal());
        return PageDTO.of(page);
    }

    @Override
    public void update(OrderDTO orderDTO) {
        Order order = BeanUtil.copyProperties(orderDTO, Order.class);
        this.updateById(order);
    }

    @Override
    public List<OrderVO> getAllOrders() {
        List<Order> orders = this.list();
        List<Long> customerIds = orders.stream()
                .map(Order::getCustomerId)
                .collect(Collectors.toList());
        List<Customer> customers = customerService.listByIds(customerIds);
        Map<Long, Customer> customerMap = customers.stream()
                .collect(Collectors.toMap(Customer::getId, customer -> customer));
        List<Long> productIds = orders.stream()
                .map(Order::getProductId)
                .collect(Collectors.toList());
        List<Product> products = productService.listByIds(productIds);
        Map<Long, Product> productMap = products.stream()
                .collect(Collectors.toMap(Product::getId, product -> product));
        return orders.stream()
                .map(order -> {
                    OrderVO vo = new OrderVO();
                    BeanUtils.copyProperties(order, vo);
                    // 获取客户名称
                    if (order.getCustomerId() != null) {
                        Customer customer = customerMap.get(order.getCustomerId());
                        if (customer != null) {
                            vo.setCustomerName(customer.getCustomerName());
                        }
                    }
                    // 获取产品名称
                    if(order.getProductId() != null){
                        Product product = productMap.get(order.getProductId());
                        if(product != null){
                            vo.setProductName(product.getName());
                        }
                    }
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<OrderVO> getOrderListByQuery(OrderPageQuery query) {
        Page<OrderVO> page = this.baseMapper.getOrderList(query);
        return page.getResult();
    }
}
